﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PushpinTypeBrushConverter.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Defines the PushpinTypeBrushConverter type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.Silverlight.Converters
{
    using System;
    using System.Windows.Data;
    using System.Windows.Media;

    /// <summary>
    /// Defines the PushpinTypeBrushConverter type
    /// </summary>
    public class PushpinTypeBrushConverter : IValueConverter
    {
        /// <summary>
        /// Default brush for unrecognized pushpins
        /// </summary>
        private static readonly Brush DefaultBrush;

        /// <summary>
        /// Initializes static members of the <see cref="PushpinTypeBrushConverter"/> class.
        /// </summary>
        static PushpinTypeBrushConverter()
        {
            DefaultBrush = new SolidColorBrush(Color.FromArgb(0xFF, 0x37, 0x84, 0xDF));
        }

        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>
        /// The value to be passed to the target dependency property.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var rgbHex = value as string;
            if (rgbHex == null || rgbHex.Length != 6)
            {
                return DefaultBrush;
            }

            var r = System.Convert.ToByte(rgbHex.Substring(0, 2), 16);
            var g = System.Convert.ToByte(rgbHex.Substring(2, 2), 16);
            var b = System.Convert.ToByte(rgbHex.Substring(4, 2), 16);
            var brush = new SolidColorBrush(Color.FromArgb(0xFF, r, g, b));
            return brush;
        }

        /// <summary>
        /// Modifies the target data before passing it to the source object.  This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings.
        /// </summary>
        /// <param name="value">The target data being passed to the source.</param>
        /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>
        /// The value to be passed to the source object.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
